﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using ComponentAce.Compression.Libs.zlib;
using SimpleClient.NetManager;

namespace SimpleClient
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox
            // 
            this.textBox.Location = new System.Drawing.Point(151, 12);
            this.textBox.Name = "textBox";
            this.textBox.Size = new System.Drawing.Size(100, 21);
            this.textBox.TabIndex = 0;
            this.textBox.Text = "看吧～";
            this.textBox.TextChanged += new System.EventHandler(this.textBox_TextChanged);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(135, 316);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(135, 34);
            this.button1.TabIndex = 0;
            this.button1.Text = "登录";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(12, 416);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(135, 34);
            this.button2.TabIndex = 1;
            this.button2.Text = "发送一百条数据";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(255, 416);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(135, 34);
            this.button3.TabIndex = 2;
            this.button3.Text = "发送一万条数据";
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(402, 462);
            this.Controls.Add(this.textBox);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "CompressFile demo (c) ComponentAce 2006";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
        {
            byte[] buffer = new byte[2000];
            int len;
            while ((len = input.Read(buffer, 0, 2000)) > 0)
            {
                output.Write(buffer, 0, len);
            }
            output.Flush();
        }

        private void compressFile(string inFile, string outFile)
        {
            System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
            ZOutputStream outZStream = new ZOutputStream(outFileStream, zlibConst.Z_DEFAULT_COMPRESSION);
            System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
            try
            {
                CopyStream(inFileStream, outZStream);
            }
            finally
            {
                outZStream.Close();
                outFileStream.Close();
                inFileStream.Close();
            }
        }

        private void decompressFile(string inFile, string outFile)
        {
            System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
            ZOutputStream outZStream = new ZOutputStream(outFileStream);
            System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
            try
            {
                CopyStream(inFileStream, outZStream);
            }
            finally
            {
                outZStream.Close();
                outFileStream.Close();
                inFileStream.Close();
            }
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            wmPacket oPacket = new wmPacket();
            oPacket.SetOpcode(0);
            oPacket.SetRetCode(0);

            NetMgr.getInstance().Send(oPacket);
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            for (uint i = 0; i < 100; ++i)
            {
                wmPacket oPacket = new wmPacket();
                oPacket.SetOpcode(1);
                oPacket.SetRetCode(0);

                oPacket.WriteUint32(i);
                oPacket.WriteBoolean(false);
                oPacket.WriteDouble((double)123);
                oPacket.WriteFloat((float)123);
                oPacket.WriteUint8((byte)123);
                oPacket.WriteUint16((ushort)123);
                string str = "你好123abc/.,?><？？‘；";
                oPacket.WriteString(ref str);
                NetMgr.getInstance().Send(oPacket);
            }
        }

        private void button3_Click(object sender, System.EventArgs e)
        {
            for (uint i = 0; i < 10000; ++i)
            {
                wmPacket oPacket = new wmPacket();
                oPacket.SetOpcode(2);
                oPacket.SetRetCode(0);

                oPacket.WriteUint32(i);
                oPacket.WriteBoolean(false);
                oPacket.WriteDouble((double)123);
                oPacket.WriteFloat((float)123);
                oPacket.WriteUint8((byte)123);
                oPacket.WriteUint16((ushort)123);
                string str = "你好123abc/.,?><？？‘；";
                oPacket.WriteString(ref str);
                NetMgr.getInstance().Send(oPacket);
            }
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            if (NetMgr.getInstance().Connect("192.168.10.18", 8080))
            {
                Console.WriteLine("连接到服务器");
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
